//
//  HomeHeaderViewController.m
//  MMLOOK
//
//  Created by dllo on 15/9/4.
//  Copyright (c) 2015年 武俊娥. All rights reserved.
//

#import "HomeHeaderViewController.h"
#import "AsynConnect.h"
#import "HomeCollectionViewCell.h"
#import "HomeDeatViewController.h"

@interface HomeHeaderViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain)UICollectionView *collection;
@property (nonatomic, retain)NSMutableArray *homeArray;


@end

@implementation HomeHeaderViewController

- (void)dealloc
{
    [_homeArray release];
    [_collection release];
    [_homedic release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.homedic = [NSMutableDictionary dictionary];
        self.homeArray = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self Connection];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatCollection];
}

- (void)creatCollection
{
    UICollectionViewFlowLayout *layout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    layout.itemSize = CGSizeMake(350, 200);
    layout.minimumInteritemSpacing = 15;
    layout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
    
    self.collection = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout] autorelease];
    _collection.dataSource = self;
    _collection.delegate = self;
    _collection.backgroundColor = [UIColor whiteColor];
    [_collection registerClass:[HomeCollectionViewCell class] forCellWithReuseIdentifier:@"home"];
    [self.view addSubview:_collection];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _homeArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"home" forIndexPath:indexPath];
    NSMutableDictionary *dic = [_homeArray objectAtIndex:indexPath.row];
    [cell sendDic:dic];
    return cell;
}

- (void)Connection
{
    [AsynConnect asynConnectWithUrlstr:[NSString stringWithFormat:@"http://api.liwushuo.com/v1/collections/%@/posts?gender=2&generation=1&limit=20&offset=0", [self.homedic objectForKey:@"target_id"]] parmater:nil block:^(NSData *data) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *littleDic = [dic objectForKey:@"data"];
        NSMutableArray *arr = [littleDic objectForKey:@"posts"];
        [self.homeArray addObjectsFromArray:arr];
        [_collection reloadData];
    }];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeDeatViewController *homedeta = [[[HomeDeatViewController alloc] init] autorelease];
    homedeta.dic = [_homeArray objectAtIndex:indexPath.item];
    [self.navigationController pushViewController:homedeta animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
